#include "MessageEdit.h"

MessageEdit::MessageEdit(Board * board, QString title, QWidget * parent) : QWidget(parent, Qt::Window){
    if (board == 0)
	return;
    else
	_board = board;
    if (title == "")
	_title = tr("new post");
    else
	_title = title;
    setupUi(this);
    editSignature->setCurrentIndex(0);
    set(_title, "");
}

void MessageEdit::on_sendButton_clicked(){
    QString title = editTitle->text();
    QString signature = editSignature->currentText();
    QString text = editMessage->toPlainText();

    if (title == "") title = _title;
    if (signature == "") signature = "1";
    if (text == "") text = "...";

    _board -> send(title, signature, text);
    close();
}

void MessageEdit::set(QString title, QString text){
    _title = title;
    editTitle->setText(_title);
    editMessage->setText(text);
    QSettings settings (PROGRAM_COMP, PROGRAM_NAME);
    settings.beginGroup("MessageEdit");
    restoreGeometry(settings.value("geometry").toByteArray());
    settings.endGroup();
    show();
    raise();
    activateWindow();
}

void MessageEdit::closeEvent(QCloseEvent * event){
    QSettings settings (PROGRAM_COMP, PROGRAM_NAME);
    settings.beginGroup("MessageEdit");
    settings.setValue("geometry", saveGeometry());
    settings.endGroup();
    event-> accept();
}
